<?php
//接收页面
header('content-type:text/html;charset=utf-8');

$mysqli=new mysqli('localhost','root','root','test');
if($mysqli->connect_errno){
    die($mysqli->connect_error);
}
$mysqli->set_charset("utf8");

$username=$_POST['username'];
$username=$mysqli->escape_string($username);
$password=md5($_POST['password']);
$age=$_POST['age'];
$id=$_GET['id'];
$act=$_GET['act'];
//根据不同操作完成不同功能
switch ($act){
    case 'add':
        //echo '添加用户操作';
        $sql = "INSERT user(username,password,age) VALUES('${username}','${password}','${age}')";
        $res = $mysqli->query($sql);
        if($res){
            $insert_id = $mysqli->insert_id;
            echo "<script type='text/javascript'>
                    alert('添加成功，网站第{$insert_id}位用户');
                    location.href='userlist.php';
                  </script>";
            exit;
            
        }else{
            echo "<script type='text/javascript'>
            alert('添加失败，重新添加');
            location.href='user.php';
            </script>";
            exit;
        }
        break;
    case 'del':
        //echo '删除记录'.$id
        $sql = "DELETE FROM user WHERE id=".$id;
        $res=$mysqli->query($sql);
        if($res){
            $msg="删除成功";
        }else{
            $msg="删除失败";
        }
        $url = 'userlist.php';
        echo "<script type='text/javascript'>alert('${msg}');
        location.href='${url}';
        </script>";
        exit;
        break;
    case 'edit':
        $sql = "UPDATE user SET username='{$username}',password='{$password}',age=${age} WHERE id=".$id;
        $res = $mysqli->query($sql);
        if($res){
            $msg="更新成功";
        }else{
            $msg="更新失败";
        }
        $url = 'userlist.php';
        echo "<script type='text/javascript'>alert('${msg}');
        location.href='${url}';
        </script>";
        exit;
        break;
}